<template>
    <div class="modify-password-container">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span style="font-weight: 700; font-size: 18px;">修改密码</span>
            </div>
            <div>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="新密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item>

                    </el-form-item>
                </el-form>
                <div style="text-align: center">
                    <el-button @click="submitForm('ruleForm')" style="background-color: #83AF9B; color: white;">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </div>
            </div>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: 'ModifyPassword',
        data() {
            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            const validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                }
            };
        },
        created() {
            localStorage.getItem('username')
            console.log( "username",localStorage.getItem('username'))
        },
        methods: {
            submitForm(formName) {
                const _this = this
                console.log(localStorage.getItem('username'),_this.ruleForm.pass,_this.ruleForm.checkPass)
                this.$refs[formName].validate((valid) => {
                    let param = new URLSearchParams();
                    param.append("username", localStorage.getItem('username'));
                    param.append("password1", _this.ruleForm.pass);
                    param.append("password2", _this.ruleForm.checkPass)
                    if (valid) {
                        _this.$http.post('user/modifyPassword', param).then(function (resp) {
                            console.log(resp)
                            if(resp.data.code === 203)
                            {
                                _this.$message.success(resp.data.data);
                            }
                            else{
                                _this.$message.error("密码修改失败")
                                _this.resetForm(formName)
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>
    .text {
        font-size: 14px;
    }

    .item {
        margin-bottom: 18px;
    }

    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }
    .clearfix:after {
        clear: both
    }

    .box-card {
        width: 100%;
    }
</style>
